package com.foo.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.example.www.FacadeInterfaceServiceStub.GetNewsByCategoryFault;
import org.example.www.FacadeInterfaceServiceStub.GetNewsByKeywordFault;
import org.example.www.FacadeInterfaceServiceStub.NewsFacade;
import org.example.www.FacadeInterfaceServiceStub.NewsFacadeList;
import org.example.www.FacadeWSClient;
import org.example.www.GetNewsByCategoryFaultException;
import org.example.www.GetNewsByKeywordFaultException;

/**
 * Servlet implementation class NewsFacadeServlet
 */
public class NewsFacadeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsFacadeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher;
		HttpSession session = request.getSession(true);
		NewsFacadeList newsArray = null;
		
		session.setAttribute("NewsData", null);
		FacadeWSClient client = new FacadeWSClient();
		
		String sortBased = request.getParameter("SortBased");
		
		if(sortBased.equalsIgnoreCase("newsbykeyword"))
		{
			System.out.println("newbykeyword called!");
			String Keyword = request.getParameter("newsbykeyword");
			if(Keyword.equalsIgnoreCase(""))
			{
				request.setAttribute("ServerMessage","Please Enter value in required* fields!");
				dispatcher = getServletContext().getRequestDispatcher("/news.jsp");
				dispatcher.forward(request, response);
			}
			try 
			{
				newsArray = client.getNewsByKeyword(Keyword);
			} 
			catch (GetNewsByKeywordFaultException e) 
			{
				GetNewsByKeywordFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetNewsByKeywordFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/news.jsp");
				dispatcher.forward(request, response);
			}
		}
		else if(sortBased.equalsIgnoreCase("newsbycategory"))
		{
			System.out.println("newbycategory called!");
			String Category = request.getParameter("newsbycategory");
			try {
				newsArray = client.getNewsByCategory(Category);
			} catch (GetNewsByCategoryFaultException e) {
				GetNewsByCategoryFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetNewsByCategoryFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/news.jsp");
				dispatcher.forward(request, response);
			}
		}
		
		NewsFacade[] news =null;
		if(newsArray!=null)
			{
			System.out.println("array is not null.");
			news = newsArray.getNews_list();
			}
		
		if(sortBased.equalsIgnoreCase("newsbycategory"))
		{
			String Category = request.getParameter("newsbycategory");
			for(int i=0;i<news.length;i++)
			{
				news[i].setNews_category(Category);
			}
		}
		System.out.println("setting attribute and navigate to another page!");
		session.setAttribute("NewsData", news);
		dispatcher = getServletContext().getRequestDispatcher("/news.jsp");
		dispatcher.forward(request, response);	
	}

}
